package com.zzyl.controller;

import com.aliyun.iot20180120.Client;
import com.aliyun.iot20180120.models.*;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.properties.AliIoTConfigProperties;
import com.zzyl.service.DeviceService;
import com.zzyl.vo.DeviceVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author sjqn
 * @date 2023/11/13
 */
@RestController
@RequestMapping("/iot")
public class DeviceController {

    @Autowired
    private Client client;

    @Autowired
    private AliIoTConfigProperties aliIoTConfigProperties;

    @PostMapping("/QueryProductList")
    public ResponseResult queryProductList(@RequestBody QueryProductListRequest request) throws Exception {
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        QueryProductListResponse response = client.queryProductList(request);
        if(response.getBody().success){
            return  ResponseResult.success(response.getBody().getData());
        }
        return  ResponseResult.success();
    }

    @Autowired
    private DeviceService deviceService;

    @PostMapping("/RegisterDevice")
    public ResponseResult registerDevice(@RequestBody DeviceDto deviceDto) throws Exception {
        return deviceService.registerDevice(deviceDto);
    }

    @PostMapping("/QueryDevice")
    public ResponseResult<PageResponse<DeviceVo>> queryDevice(@RequestBody QueryDeviceRequest request) throws Exception {
        return deviceService.queryDevice(request);
    }

    @PostMapping("/QueryDeviceDetail")
    public ResponseResult queryDeviceDetail(@RequestBody QueryDeviceDetailRequest request) throws Exception {
        return deviceService.queryDeviceDetail(request);
    }

    @PostMapping("/QueryDevicePropertyStatus")
    public ResponseResult queryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) throws Exception {
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        QueryDevicePropertyStatusResponse response = client.queryDevicePropertyStatus(request);
        return ResponseResult.success(response.getBody().getData());
    }

    @PostMapping("/QueryThingModelPublished")
    public ResponseResult queryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request) throws Exception {
        request.setIotInstanceId(aliIoTConfigProperties.getIotInstanceId());
        QueryThingModelPublishedResponse response = client.queryThingModelPublished(request);
        return ResponseResult.success(response.getBody().getData());
    }

    @PostMapping("/UpdateDevice")
    @ApiOperation(value = "修改设备备注名称", notes = "批量修改设备备注名称")
    public ResponseResult batchUpdateDevice(@RequestBody DeviceDto deviceDto) throws Exception {
        deviceService.updateDevice(deviceDto);
        return ResponseResult.success();
    }

    @DeleteMapping("/DeleteDevice")
    @ApiOperation(value = "删除设备", notes = "删除设备")
    public ResponseResult deleteDevice(@RequestBody DeleteDeviceRequest request) throws Exception {
        deviceService.deleteDevice(request);
        return ResponseResult.success();
    }




}
